import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import 'package:flutter_ume_kit_mock/data/model/request.dart';

part 'mock.g.dart';

@JsonSerializable()
class Mock {
  String? id;
  MockFields? fields;

  @JsonKey(ignore: true)
  Request? request;

  @JsonKey(ignore: true)
  bool enable = false;

  Mock();

  factory Mock.fromJson(Map<String, dynamic> json) => _$MockFromJson(json);

  Map<String, dynamic> toJson() => _$MockToJson(this);

  @override
  String toString() {
    return jsonEncode(this);
  }

  ///该Mock是否与其他mock冲突，即针对同一个接口，返回不同的响应
  bool isConflictWithAnother(Mock? mock) {
    if (mock == null || mock.id == id) {
      return false;
    }
    return request?.fields?.path == mock.request?.fields?.path &&
        request?.fields?.method == mock.request?.fields?.method &&
        fields?.condition == mock.fields?.condition;
  }
}

@JsonSerializable()
class MockFields {
  String? title;
  String? module;
  String? condition;
  String? tamper;
  String? response;
  String? desc;
  int? delay;
  List<MockRequest>? request;

  MockFields();

  factory MockFields.fromJson(Map<String, dynamic> json) =>
      _$MockFieldsFromJson(json);

  Map<String, dynamic> toJson() => _$MockFieldsToJson(this);

  @override
  String toString() {
    return jsonEncode(this);
  }
}

@JsonSerializable()
class MockRequest {
  List<String>? record_ids;

  MockRequest();

  factory MockRequest.fromJson(Map<String, dynamic> json) =>
      _$MockRequestFromJson(json);

  Map<String, dynamic> toJson() => _$MockRequestToJson(this);

  @override
  String toString() {
    return jsonEncode(this);
  }
}
